<?php
/**
 * 工具类
 *   
 * @author wenqiang@6rooms.com
 * @version $Id: Util.class.php,v 1.0 2013/12/5 11:28:20 gaowenqiang Exp $   
 * @package hw   
 *
 */
class Hw_Util
{
	/**
	 * 检查字符串是否为合法的日期(yyyy-mm-dd)
	 *
	 * @param string $str
	 * @return bool
	 *
	 */
	public static function isDate($str)
	{
		$tmpDate = explode('-', $str);
		if (count($tmpDate)  < 3) {
			return false;
		}
		
		list($tmpY, $tmpM, $tmpD) = $tmpDate;

		return checkdate($tmpM, $tmpD, $tmpY);
	}
	
	/**
	 * 检查字符串是否为合法的时间(hh:mm:ss)
	 *
	 * @param string $str
	 * @return bool
	 *
	 */
	public static function isTime($str)
	{
		return true;
	}
	
	/**
	 * 检查字符串是否为合法的日期时间(yyyy-mm-dd hh:mm:ss)
	 *
	 * @param string $str
	 * @return bool
	 *
	 */
	public static function isDatetime($str)
	{
		return true;
	}
	
	// str1: 2013-12-5
	// str2: 2013-12-5[ |#][0-23]
	// str3: 2013-12-5[ |#][0-23]:[0|30]
	// str4: 2013-12-5[ |#][0-23]:[0|30]:[0-59]
	public static function str2Date($str)
	{
		return self::str2Datetime($str, 2);
	}

	public static function str2Time($str)
	{
		return self::str2Datetime($str, 3);
	}

	public static function str2Datetime($str, $type = 1)
	{
		$date = array(
			'y' => null,
			'm' => null,
			'd' => null,
			'h' => null,
			'i' => null,
			's' => null,
		);
		
		if ($type === 1) {// datetime
			if (self::isDatetime($str)) {
					
			}
		} elseif ($type === 2) {// date

		} elseif ($type === 3) {// time
		
		} else {
		
		}
	}

	public static function isHour($str)
	{
		return (int)$str >= 0 && ($str) <= 23;
	}
	
	public static function isSecond($str)
	{
		return (int)$str >= 0 && ($str) <= 59;
	}

	public static function datecmp($str1, $str2) {
		$time1 = is_numeric($str1) ? $str1 + 0 : strtotime($str1);
		$time2 = is_numeric($str2) ? $str1 + 0 : strtotime($str2);

		return $time1 > $time2 ? 1 : ($time2 > $time1 ? -1 : 0);
	}

}